package note.sort;

import note.pojo.Date;
import note.pojo.Words;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Comparator;


/**
 * @program: LeetCodePractice
 * @description: 测试Java中提供的排序的API
 * @author: WongKee
 * @create: 2020-08-27 09:38

对基本数据类型进行排序
sort(byte[] a)      sort(byte[] a,int fromIndex,int toIndex)
sort(char[] a)      sort(char[] a, int fromIndex, int toIndex)
sort(double[] a)    sort(double[] a, int fromIndex, int toIndex)
sort(float[] a)     sort(float[] a, int fromIndex, int toIndex)
sort(int[] a)       sort(int[] a, int fromIndex, int toIndex)
sort(long[] a)      sort(long[] a, int fromIndex, int toIndex)
sort(short[] a)     sort(short[] a, int fromIndex, int toIndex)

对指定对象进行排序（跟根据对象的自然排序（实现Comparable接口的类的compareTo方法被称为自然比较方法））
Collections.sort和Arrays.sort可以自动对实现Comparable接口的对象进行列表和数组排序
sort(Object[] a)    sort(Object[] a, int fromIndex, int toIndex)

用指定的比较器引发的顺序对指定的对象数组进行排序
sort(T[] a, Comparator<? super T> c)
sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c)
 **/
public class JavaSort {
    public static void main(String[] args) {
        Date date=new Date(11,12,2018);
        Date date1=new Date(10,12,2018);
        Date date2=new Date(11,11,2018);
        Date date3=new Date(11,12,2019);
        Date date4=new Date(11,12,2017);

        Date[] dates=new Date[]{date,date1,date2,date3,date4};
       // testObjectComparable(dates);

        Words word1=new Words("abc");
        Words word2=new Words("cba");
        Words word3=new Words("abcd");
        Words word4=new Words("bac");

        Words[] words={word1,word2,word3,word4};
        testTComparator(words);
    }


    public static void testObjectComparable(Date[] dates){
        Arrays.sort(dates);  //sort(Object[] a)
        for(Date element:dates){
            System.out.println(element);
        }
    }
    public static void testTComparator(Words[] words){
        /*
        Arrays.sort(words, new Comparator<Words>() {
            @Override
            public int compare(Words o1, Words o2) {
                int len1=o1.word.length();
                int len2=o2.word.length();
                int len=len1<len2?len1:len2;

                char[] arr1=o1.word.toCharArray();
                char[] arr2=o2.word.toCharArray();
                for(int i=0;i<len;i++){
                    if(arr1[i]<arr2[i]){
                        return -1;
                    }

                    if(arr1[i]>arr2[i]){
                        return 1;
                    }
                }

                return len1-len2;
            }
        });*/

        //使用lambda表达式实现
        Arrays.sort(words,(o1,o2)->{
            int len1=o1.word.length();
            int len2=o2.word.length();
            int len=len1<len2?len1:len2;

            char[] arr1=o1.word.toCharArray();
            char[] arr2=o2.word.toCharArray();
            for(int i=0;i<len;i++){
                if(arr1[i]<arr2[i]){
                    return -1;
                }

                if(arr1[i]>arr2[i]){
                    return 1;
                }
            }

            return len1-len2;
        });
        for(Words word:words){
            System.out.println(word);
        }
    }

}
